/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converters;

import entities.TipoAlmacen;
import facades.TipoAlmacenFacade;
import javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;

/**
 *
 * @author damfercer
 */
@Named("tipoAlmacenConverter")
@ApplicationScoped
public class TipoAlmacenConverter implements Converter{
    @Inject
    private TipoAlmacenFacade tipoAlmacenFacade;
    /**
     * Creates a new instance of DConverter
     */
    public TipoAlmacenConverter(){
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        TipoAlmacen tipoAlmacen=null;
        try{
        Integer identificador=new Integer(value);
        tipoAlmacen=tipoAlmacenFacade.find(identificador);
        }
        catch(NumberFormatException e){
            
        }
        return tipoAlmacen;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String res=null;
        TipoAlmacen tipoAlmacen=null;
        if(value.getClass().equals(TipoAlmacen.class)){
            tipoAlmacen=(TipoAlmacen)value;
        }
        if(tipoAlmacen!=null){
            res=tipoAlmacen.getIdTipoAlmacen().toString();
        }
        return res;
    }
}
